package com.vastcom.sigma.admin.service.impl;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.vastcom.sigma.admin.dao.*;
import com.vastcom.sigma.admin.model.*;
import com.vastcom.sigma.admin.service.ManageService;
import com.vastcom.sigma.admin.to.BlockChain;
import com.vastcom.sigma.admin.tools.Tools;
import com.vastcom.sigma.admin.vo.NzSelOption;
import com.vastcom.sigma.admin.vo.NzTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.util.*;

@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private GysDao gysDao;

    @Autowired
    private ReferDataDao referDataDao;

    @Autowired
    private ReferTypeDao referTypeDao;

    @Autowired
    private CargocateDao cargocateDao;

    @Autowired
    private CoffeeincomingDao coffeeincomingDao;

    @Autowired
    private MatincomingDao matincomingDao;

    @Autowired
    private ProplanDao proplanDao;

    @Autowired
    private DbdDao dbdDao;

    @Autowired
    private BakerecordDao bakerecordDao;

    @Autowired
    private BaketestDao baketestDao;

    @Autowired
    private EncapsortDao encapsortDao;

    @Autowired
    private CuptestDao cuptestDao;

    @Autowired
    private EncapsulationDao encapsulationDao;

    @Autowired
    private ProhouseidDao prohouseDao;

    @Autowired
    private ProdoutboundDao prodoutboundDao;

    @Autowired
    private KcslDao kcslDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private UpblockChainDao upblockChainDao;

    @Autowired
    private BlockChaindbDao blockChaindbDao;


    @Autowired
    private RawBeanInventoryDao rawBeanInventoryDao;
    @Autowired
    private RawBeanInventoryChangeDao rawBeanInventoryChangeDao;

    @Autowired
    private MateriainventoryDao materiainventoryDao;
    @Autowired
    private MateriainventorychangeDao materiainventorychangeDao;

    @Autowired
    private LoginlogDao loginlogDao;

    @Autowired
    private SearchLogDao searchLogDao;


    @Override
    public Gys saveGys(Gys gys) {
        try {
            gysDao.save(gys);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteGysByid(String id) {
        try {
            gysDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Gys> getGyses() {
        return gysDao.findAll();
    }

    @Override
    public String getGysNameByid(String id) {
        Gys gysByid = this.getGysByid(id);
        if (gysByid != null)
            return gysByid.getMc();
        else
            return "";
    }

    @Override
    public Gys getGysByid(String id) {
        Optional<Gys> byId = gysDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public Refertype saveRefertype(Refertype refertype) {
        try {
            referTypeDao.save(refertype);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteRefertypeByid(String id) {
        try {
            referTypeDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Refertype getRefertypeByid(String id) {
        Optional<Refertype> byId = referTypeDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public List<Refertype> getRefertypes() {

        return referTypeDao.getRefertypesOrOrderByBasecode();
    }

    @Override
    public Referdata saveReferdata(Referdata referdata) {
        try {
            referDataDao.save(referdata);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteReferdataByid(String id) {
        try {
            referDataDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Referdata getReferdataByid(String id) {
        Optional<Referdata> byId = referDataDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public String getReferdateNameByid(String id) {
        Referdata referdataByid = this.getReferdataByid(id);
        if (referdataByid != null)
            return referdataByid.getName();
        return "";
    }

    @Override
    public List<Referdata> getReferdatas() {
        return referDataDao.findAll();
    }

    @Override
    public List<Referdata> getReferdatasByTypeid(String id) {
        return referDataDao.getReferdatasByTypeid(id);
    }

    @Override
    public List<NzSelOption> getReferdataSelectByTypeid(String id) {
        List<Referdata> referdatasByTypeid = this.getReferdatasByTypeid(id);
        List<NzSelOption> result = new ArrayList<>();
        for (Referdata referdata : referdatasByTypeid) {
            NzSelOption nzSelOption = new NzSelOption();
            nzSelOption.setLabel(referdata.getName());
            nzSelOption.setValue(referdata.getReferdata());
            result.add(nzSelOption);
        }
        return result;
    }

    @Override
    public Cargocate saveCargocate(Cargocate cargocate) {
        try {
            cargocateDao.save(cargocate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteCargocate(String id) {
        try {
            cargocateDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Cargocate getCargocateByid(String id) {
        Optional<Cargocate> byId = cargocateDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public List<NzTreeVo> getNzTreeCargocate(String pid) {
        List<NzTreeVo> result = new ArrayList<>();
        List<Cargocate> cargocatesByCargocateid = this.getCargocatesByCargocateid(pid);
        if (cargocatesByCargocateid.size() != 0) {
            for (Cargocate cargocate : cargocatesByCargocateid) {
                if (cargocate.getPid().equals(pid)) {
                    NzTreeVo nzTreeVo = new NzTreeVo();
                    nzTreeVo.setTitle(cargocate.getName());
                    nzTreeVo.setKey(cargocate.getCargocateid());
                    nzTreeVo.setLeaf(true);
                    List<NzTreeVo> nzTreeCargocate = this.getNzTreeCargocate(cargocate.getCargocateid());
                    if (nzTreeCargocate.size() != 0) {
                        nzTreeVo.setChildren(nzTreeCargocate);
                        nzTreeVo.setExpanded(false);
                    } else {
                        nzTreeVo.setExpanded(false);
                        nzTreeVo.setLeaf(true);
                    }
                    result.add(nzTreeVo);
                }
            }
        }
        return result;
    }

    @Override
    public String getCargocateNamwByid(String id) {
        Cargocate cargocateByid = this.getCargocateByid(id);
        if (cargocateByid != null) {
            return cargocateByid.getName();
        }
        return "";
    }

    @Override
    public List<NzSelOption> getNzSelOptionCargocate(String pid) {
        List<Cargocate> cargocates = this.getCargocates();
        List<NzSelOption> result = new ArrayList<>();
        for (Cargocate cargocate : cargocates) {
            if (!Tools.isNullOrSpace(cargocate.getPid()))
                if (cargocate.getPid().equals(pid)) {
                    NzSelOption nzSelOption = new NzSelOption();
                    nzSelOption.setLabel(cargocate.getName());
                    nzSelOption.setValue(cargocate.getCargocateid());
                    result.add(nzSelOption);
                }
        }
        return result;
    }

    private List<Cargocate> getCargocatesByCargocateid(String id) {
        return cargocateDao.getCargocatesByCargocateid(id);
    }

    @Override
    public List<Cargocate> getCargocates() {
        return cargocateDao.findAll();
    }

    @Override
    public List<Cargocate> getCargocatesBypid(String cargocateid) {
        return cargocateDao.getCargocatesByCargocateid(cargocateid);
    }

    @Override
    public Coffeeincoming saveCoffeeincoming(Coffeeincoming coffeeincoming) {
        try {
            coffeeincomingDao.save(coffeeincoming);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteCoffeeincomingByid(String id) {
        try {
            coffeeincomingDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Coffeeincoming getCoffeeincomingByid(String id) {
        Optional<Coffeeincoming> byId = coffeeincomingDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public Coffeeincoming getCoffeeincomingByPm(String id) {
        List<Coffeeincoming> coffeeincomingsByPm = this.getCoffeeincomingsByPm(id);
        return coffeeincomingsByPm.get(0);
    }

    @Override
    public List<Coffeeincoming> getCoffeeincomings() {
        return coffeeincomingDao.findAll();
    }

    @Override
    public List<Coffeeincoming> getCoffeeincomingsByPm(String pm) {
        return coffeeincomingDao.getCoffeeincomingsByPmOrderByJhrq(pm);
    }

    @Override
    public Object[] getRawBeanNum() {

        return coffeeincomingDao.countCoffeeincomingByPm();

        //return new Object[0];
    }

    @Override
    public Matincoming saveMatincoming(Matincoming matincoming) {
        try {
            matincomingDao.save(matincoming);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Matincoming getMatincomingByid(String id) {
        Optional<Matincoming> byId = matincomingDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public List<Matincoming> getMatincomings() {
        return matincomingDao.findAll();
    }

    @Override
    public List<Matincoming> getMatincomingsByPm(String pm) {
        return matincomingDao.getMatincomingsByPm(pm);
    }

    @Override
    public boolean deleteMatincomingByid(String id) {
        try {
            matincomingDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Proplan saveProplan(Proplan proplan) {
        try {
            proplanDao.save(proplan);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Proplan getProplanByid(String id) {
        Optional<Proplan> byId = proplanDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public List<Proplan> getProplans() {
        return proplanDao.getProplansOrOrderByJhrqAsc();
    }

    @Override
    public boolean deleteProplanByid(String id) {
        try {
            proplanDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Dbd saveDbd(Dbd dbd) {
        try {
            dbdDao.save(dbd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Dbd getDbdByid(String id) {
        Optional<Dbd> byId = dbdDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public Dbd getDbdByScpcAndBC(String id) {
        List<Dbd> dbds = this.getDbds();
        for (Dbd dbd : dbds) {
            if (dbd.getScpc().equals(id) && dbd.getType().equals("包材")) {
                return dbd;
            }
        }
        return null;
    }

    @Override
    public Dbd getDbdByScpc(String id) {
        List<Dbd> dbds = this.getDbds();
        for (Dbd dbd : dbds) {
            if (dbd.getScpc().equals(id) ) {
                return dbd;
            }
        }
        return null;
    }

    @Override
    public Dbd getDbdByScpcAndCoffee(String id) {
        List<Dbd> dbds = this.getDbds();
        for (Dbd dbd : dbds) {
            if (dbd.getScpc().equals(id) && dbd.getType().equals("咖啡")) {
                return dbd;
            }
        }
        return null;
    }

    @Override
    public List<Dbd> getDbds() {
        return dbdDao.getDbdsByCkrq();
    }

    @Override
    public List<Dbd> getDbdsByClqk(String clqk) {
        return dbdDao.getDbdsByClqk(clqk);
    }

    @Override
    public boolean deleteDbdByid(String id) {
        try {
            dbdDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    @Override
    public Bakerecord saveBakerecord(Bakerecord bakerecord) {
        try {
            bakerecordDao.save(bakerecord);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Bakerecord getBakerecordByid(String id) {
        Optional<Bakerecord> byId = bakerecordDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public Bakerecord getBakerecordByScpc(String scpc) {
        List<Bakerecord> bakerecordsByScpc = bakerecordDao.getBakerecordsByScpc(scpc);
        if(bakerecordsByScpc.isEmpty()){
            return null;
        }
        return bakerecordsByScpc.get(0);
    }

    @Override
    public List<Bakerecord> getBakerecords() {
        return bakerecordDao.getBakerecordsOrderByHbrq();
    }

    @Override
    public List<Bakerecord> getBakerecordsByClqk(String clqk) {
        return bakerecordDao.getBakerecordsByClqk(clqk);
    }

    @Override
    public boolean deleteBakerecordByid(String id) {
        try {
            bakerecordDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Baketest saveBaketest(Baketest baketest) {
        try {
            baketestDao.save(baketest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Baketest getBaketestByid(String id) {
        Optional<Baketest> byId = baketestDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public Baketest getBaketestByScpc(String scpc) {
        List<Baketest> baketestsByScpc = baketestDao.getBaketestsByScpc(scpc);
        if(baketestsByScpc.isEmpty()){
            return null;
        }
        return baketestsByScpc.get(0);
    }

    @Override
    public List<Baketest> getBaketests() {
        return baketestDao.findAll();
    }

    @Override
    public List<Baketest> getBaketestsByClqk(String clqk) {
        return baketestDao.getBaketestsByClqk(clqk);
    }

    @Override
    public boolean deleteBaketestByid(String id) {
        try {
            baketestDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Encapsort saveEncapsort(Encapsort encapsort) {
        try {
            encapsortDao.save(encapsort);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Encapsort getEncapsortByid(String id) {
        Optional<Encapsort> byId = encapsortDao.findById(id);

        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public Encapsort getEncapsortByScpc(String id) {
        List<Encapsort> encapsorts = this.getEncapsorts();
        for (Encapsort encapsort : encapsorts) {
            if (encapsort.getScpc().equals(id)) {
                return encapsort;
            }
        }
        return null;
    }

    @Override
    public List<Encapsort> getEncapsorts() {
        return encapsortDao.getEncapsortsByFjsj();
    }

    @Override
    public List<Encapsort> getEncapsortsByClqk(String clqk) {
        return encapsortDao.getEncapsortsByClqk(clqk);
    }

    @Override
    public boolean deleteEncapsortstByid(String id) {
        try {
            encapsortDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Cuptest saveCuptest(Cuptest cuptest) {
        try {
            cuptestDao.save(cuptest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Cuptest getCuptestByid(String id) {
        Optional<Cuptest> byId = cuptestDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public Cuptest getCuptestByScpc(String scpc) {
        List<Cuptest> cuptestsByScpc = cuptestDao.getCuptestsByScpc(scpc);
        if(cuptestsByScpc.isEmpty()){
            return null;
        }
        return cuptestsByScpc.get(0);
    }

    @Override
    public List<Cuptest> getCuptests() {
        return cuptestDao.findAll();
    }

    @Override
    public List<Cuptest> getCuptestsByClqk(String clqk) {
        return cuptestDao.getCuptestsByClqk(clqk);
    }

    @Override
    public boolean deleteCuptestByid(String id) {
        try {
            cuptestDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    @Override
    public Encapsulation saveEncapsulation(Encapsulation encapsulation) {
        try {
            encapsulationDao.save(encapsulation);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Encapsulation getEncapsulationByid(String id) {
        Optional<Encapsulation> byId = encapsulationDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public Encapsulation getEncapsulationByScpc(String id) {
        List<Encapsulation> encapsulationsByScpc = encapsulationDao.getEncapsulationsByScpc(id);
        if(encapsulationsByScpc.isEmpty()){
            return null;
        }
        return encapsulationsByScpc.get(0);
    }

    @Override
    public List<Encapsulation> getEncapsulations() {
        return encapsulationDao.getEncapsulations();
    }

    @Override
    public List<Encapsulation> getEncapsulationsByClqk(String clqk) {
        return encapsulationDao.getEncapsulationsByClqk(clqk);
    }

    @Override
    public boolean deleteEncapsulationByid(String id) {
        try {
            encapsulationDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Prohouseid saveProhouseid(Prohouseid prohouseid) {
        try {
            prohouseDao.save(prohouseid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Prohouseid getProhouseidByid(String id) {
        Optional<Prohouseid> byId = prohouseDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public Prohouseid getProhouseidByScpc(String scpc) {
        List<Prohouseid> prohouseidsByScpc = prohouseDao.getProhouseidsByScpc(scpc);
        if(prohouseidsByScpc.isEmpty()){
            return null;
        }
        return prohouseidsByScpc.get(0);
    }

    @Override
    public Prohouseid getProhouseidByProducid(String id) {
        return prohouseDao.getProhouseidByProname(id);
    }

    @Override
    public List<Prohouseid> getProhouseids() {
        return prohouseDao.findAll();
    }

    @Override
    public List<Prohouseid> getProhouseidsByClqk(String clqk) {
        return prohouseDao.getProhouseidsByClqk(clqk);
    }

    @Override
    public boolean deleteProhouseidByid(String id) {
        try {
            prohouseDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Prodoutbound saveProdoutbound(Prodoutbound prodoutbound) {
        try {
            prodoutboundDao.save(prodoutbound);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Prodoutbound getProdoutboundByid(String id) {
        Optional<Prodoutbound> byId = prodoutboundDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public List<Prodoutbound> getProdoutbounds() {
        return prodoutboundDao.findAll();
    }

    @Override
    public boolean deleteProdoutboundByid(String id) {
        try {
            prodoutboundDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Kcsl saveKcsl(Kcsl kcsl) {
        try {
            kcslDao.save(kcsl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Kcsl getKcslByproid(String id) {
        List<Kcsl> all = kcslDao.findAll();
        for (Kcsl kcsl : all) {
            if (kcsl.getProid().equals(id)) {
                return kcsl;
            }
        }
        return null;
    }

    @Override
    public List<Kcsl> getKcsls() {
        return kcslDao.findAll();
    }

    @Override
    public int getKcslnumByproid(String id) {
        int kcslsByProid = kcslDao.getKcslsByProid(id);
        return kcslsByProid;
    }


    @Override
    public Rawbeaninventory saveRawbeaninventory(Rawbeaninventory rawbeaninventory) {
        try {
            return rawBeanInventoryDao.save(rawbeaninventory);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean deleteRawbeaninventory(String id) {

        try {
            rawBeanInventoryDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Rawbeaninventory getRawbeaninventoryById(String id) {
        Optional<Rawbeaninventory> temp = rawBeanInventoryDao.findById(id);

        return temp.isPresent() ? temp.get() : null;
    }

    @Override
    public Rawbeaninventory getRawbeaninventoryByPmId(String id) {
        List<Rawbeaninventory> rl = rawBeanInventoryDao.getRawbeaninventoriesByPm(id);
        return rl.isEmpty() ? null : rl.get(0);
    }

    @Override
    public List<Rawbeaninventory> getRawbeaninventoryList() {
        return rawBeanInventoryDao.findAll();
    }

    @Override
    public Rawbeaninventorychange saveRawbeaninventorychange(Rawbeaninventorychange rawbeaninventorychange) {
        try {
            return rawBeanInventoryChangeDao.save(rawbeaninventorychange);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean deleteRawbeaninventorychange(String id) {

        try {
            rawBeanInventoryChangeDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Rawbeaninventorychange getRawbeaninventorychangeById(String id) {
        Optional<Rawbeaninventorychange> temp = rawBeanInventoryChangeDao.findById(id);
        return temp.isPresent() ? temp.get() : null;
    }


    @Override
    public List<Rawbeaninventorychange> getRawbeaninventorychangeByType(String type) {
        return rawBeanInventoryChangeDao.getRawbeaninventorychangesByType(type);
    }

    @Override
    public List<Rawbeaninventorychange> getRawBeanInventoryChangeList() {
        return rawBeanInventoryChangeDao.getAllByChangedate();
    }


    @Override
    public Materiainventory saveMateriainventory(Materiainventory materiainventory) {

        try {
            return materiainventoryDao.save(materiainventory);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean deleteMateriainventory(String id) {

        try {
            materiainventoryDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    public Materiainventory getMateriainventoryById(String id) {
        Optional<Materiainventory> temp = materiainventoryDao.findById(id);
        return temp.isPresent() ? temp.get() : null;
    }

    @Override
    public Materiainventory getMateriainventoryByPm(String pm) {
        List<Materiainventory> ml = materiainventoryDao.getMateriainventoriesByPm(pm);

        return ml.isEmpty() ? null : ml.get(0);
    }

    @Override
    public List<Materiainventory> getMateriainventoryList() {
        return materiainventoryDao.findAll();
    }

    @Override
    public Materiainventorychange saveMateriainventorychange(Materiainventorychange mc) {

        try {
            return materiainventorychangeDao.save(mc);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean deleteMateriainventorychange(String id) {

        try {
            materiainventorychangeDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Materiainventorychange getMateriainventorychangeById(String id) {
        Optional<Materiainventorychange> temp = materiainventorychangeDao.findById(id);
        return temp.isPresent() ? temp.get() : null;
    }

    @Override
    public List<Materiainventorychange> getMateriainventorychangeList() {
        return materiainventorychangeDao.getMateriainventorychangesByChangedate();
    }

    @Override
    public String getCurrentUserId() {
        try {
            String userid = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (Tools.isNullOrSpace(userid)) {
                return null;
            } else {
                return userid;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public String getCurrentRole() {
        String role = null;
        Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        for (GrantedAuthority authority : authorities) {
            role = authority.getAuthority();

        }

        if (Tools.isNullOrSpace(role)) {
            return null;
        } else {
            return role;
        }

    }

    @Override
    public Member getMemberByid(String id) {
        Optional<Member> byId = memberDao.findById(id);
        return byId.isPresent() ? byId.get() : null;
    }

    @Override
    public String getMemberStrByid(String id) {
        String name = "";
        Member memberByid = this.getMemberByid(id);
        if (memberByid != null) {
            name = memberByid.getRealname();
        }
        return name;
    }

    @Override
    public Upb saveUpbloackchain(Upb upbloackchain) {
        try {


            upblockChainDao.save(upbloackchain);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteUpbloackchain(String id) {
        try {
            upblockChainDao.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Upb getUpbloackchainByid(String id) {
        Optional<Upb> byId = upblockChainDao.findById(id);
        return byId.isPresent()?byId.get():null;
    }

    @Override
    public List<Upb> getAllUpbloackchain() {
        return upblockChainDao.getAllUpbloackchain();
    }

    private static final String CHARSET = "utf-8";

    // 二维码尺寸
    private static final int QRCODE_SIZE = 300;

    @Override
    public BufferedImage createImage(String content) {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
                    hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
    }

    @Override
    public Blockchaindb saveBlockchaindb(Blockchaindb blockChain) {
        try {
            blockChaindbDao.save(blockChain);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Blockchaindb getBlockchaindbByKeyname(String keyname) {
        Blockchaindb blockchaindbByKeyname = blockChaindbDao.getBlockchaindbByKeyname(keyname);
        return blockchaindbByKeyname;
    }

    @Override
    public List<Blockchaindb> getBlockchaindbs() {
        return blockChaindbDao.getBlockchainBySlsjDesc();
    }

    @Override
    public List<Loginlog> getLoginlogs() {
        return loginlogDao.getLoginlogsByLogintime();
    }

    @Override
    public Searchlog saveSearchlog(Searchlog searchlog) {
        try {
            Searchlog save = searchLogDao.save(searchlog);
            return save;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Searchlog> getSearchlogs() {
        return searchLogDao.getSearchlogsBySearchtime();
    }
}
